/**
 * 广西南宁市国税爬虫
 *  
 */

import { Base, Item } from '../base';
class nngscg extends Base {
    constructor() {
        super();
        this.name = "广西南宁市国税采购网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex: number): Item[] {
        let url
        if (pageIndex == 0) {
            url = `http://nn.gxgs.gov.cn/xxgk/zfcg/zbgg/index.html`;
        }
        else {
            url = `http://nn.gxgs.gov.cn/xxgk/zfcg/zbgg/index_${pageIndex}.html`;
        }
        let result: Item[] = [];
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        let that = this;
        try {
            $('div.lmy_info_xxlb_list ul li').each(function (index, ele) {
                let item = that.buildItem();
                item.title = $(ele).find('a').text().trim();
                (<any>item)['采购日期'] = $(ele).find('span').eq(0).text().trim();
                item.source = that.name;
                let uri = 'http://nn.gxgs.gov.cn/xxgk/zfcg/zbgg/' + $(ele).find('a').attr('href').substring(2);
                (<any>item).url = 'http://nn.gxgs.gov.cn/xxgk/zfcg/zbgg/' + $(ele).find('a').attr('href').substring(2);
                result = that.getExtraItem(uri, item, result);
            });
        } catch (error) {
            console.log(error);
        }
        return result;
    }
    getExtraItem(url: string, item: Item, result: Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content, { decodeEntities: false });
        (<any>item).content = $('.wz_content').html();
        result.push(item);
        return result;
    }
    getPageCount() {
        return 2;
    }
}
module.exports = new nngscg();